package cn.kailangtu.serialportv2;

import androidx.annotation.NonNull;

import cn.kailangtu.serialport.CommonUtils;

public class DataVersion2 {
    private  byte[] start;
    private byte[] end;
    private int length;
    private int check;
    private int address;
    private byte[] data;

    private String cmdString;

    public byte[] getStart() {
        return start;
    }

    public void setStart(byte[] start) {
        this.start = start;
    }

    public byte[] getEnd() {
        return end;
    }

    public void setEnd(byte[] end) {
        this.end = end;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getCheck() {
        return check;
    }

    public void setCheck(int check) {
        this.check = check;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getAddress() {
        return address;
    }

    public void setAddress(int address) {
        this.address = address;
    }

    public String getCmdString() {
        return cmdString;
    }

    public void setCmdString(String cmdString) {
        this.cmdString = cmdString;
    }

    @NonNull
    @Override
    public String toString() {
        return cmdString;
    }

    public static DataVersion2 build(String cmd){
        DataVersion2 result = new DataVersion2();
        result.setCmdString(cmd);
        String strStart = cmd.substring(0,4);
        String strEnd = cmd.substring(cmd.length() - 6,cmd.length() - 2);
        String strCheck = cmd.substring(cmd.length()-2);
        String strAddress = cmd.substring(4,6);
        String strLength = cmd.substring(6,10);
        String strData = cmd.substring(10,cmd.length()-6);
        result.setStart(CommonUtils.hexStringToByteArray(strStart));
        result.setEnd(CommonUtils.hexStringToByteArray(strEnd));
        result.setCheck(CommonUtils.hexToInt(strCheck));
        result.setAddress(CommonUtils.hexToInt(strAddress));
        result.setLength(CommonUtils.hexToInt(strLength));
        result.setData(CommonUtils.hexStringToByteArray(strData));
        return result;
    }

    public static DataVersion2 build(int address,byte[] data) {
        DataVersion2 result = new DataVersion2();
        result.setData(data);
        result.setLength(data.length);
        result.setStart(new byte[]{0x7B,0x7B});
        result.setEnd(new byte[] {0x7D,0x7D});
        result.setAddress(address);
        String cmdString = CommonUtils.ByteArrayToHexString(result.getStart())
                + CommonUtils.ByteArrayToHexString( CommonUtils.int1ByteArray( address ))
                + CommonUtils.ByteArrayToHexString( CommonUtils.int2ByteArray(result.getLength()) )
                + CommonUtils.ByteArrayToHexString(result.getData())
                + CommonUtils.ByteArrayToHexString(result.getEnd());
        String strCheck = CommonUtils.getCheckValue(cmdString);
        cmdString = cmdString + strCheck;
        result.setCheck(CommonUtils.hexToInt(strCheck));
        result.setCmdString(cmdString);

        return result;
    }
}
